package com.yuji.aacar.widgets.featheredittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.EditText;

import com.yuji.aacar.R;


/**
 * <li>Package:com.ttp.widget.featheredittext
 * <li>Author: shengshujie@126.com
 * <li>Date: 2017/6/20
 * <li>Discription: The Edittext that it will be once click at all clear when input incorrect content
 */
public class WFeatherEditText extends EditText {
    private Drawable mDelete;
    private Drawable mTitle;
    private textChangeListener mTextChangeListener;
    private boolean isVisible = false;


    public WFeatherEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        intiView(context, attrs);
    }

    private void intiView(Context context, AttributeSet attrs) {
        if (null != attrs) {
            TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.WFeatherEditText);
            mDelete = array.getDrawable(R.styleable.WFeatherEditText_deletedrawable);
            mTitle = array.getDrawable(R.styleable.WFeatherEditText_frontdrawable);
            isVisible = array.getBoolean(R.styleable.WFeatherEditText_isvisible, false);
            array.recycle();
        }
        if (mDelete != null) {
            mDelete.setVisible(isVisible, true);
        }
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                setDrawable();
            }
        });
        setDrawable();
    }

    private void setDrawable() {
        if (length() < 1) {
            setCompoundDrawablesWithIntrinsicBounds(mTitle, null, null, null);
        } else {
            if (!isVisible) {
                mDelete = null;
            }
            setCompoundDrawablesWithIntrinsicBounds(mTitle, null, mDelete, null);

        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mDelete != null) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_UP:
                    int eventX = (int) event.getRawX();
                    int eventY = (int) event.getRawY();
                    Rect rect = new Rect();
                    getGlobalVisibleRect(rect);
                    rect.left = rect.right - 50;
                    if (rect.contains(eventX, eventY)) {
                        setText("");
                    }
                    break;
                default:
                    break;
            }
        }
        return super.onTouchEvent(event);
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
    }

    public interface textChangeListener {
        void setTextChange(String s);
    }

    public void setTextChangeListener(textChangeListener l) {
        mTextChangeListener = l;
    }

}
